<?php
defined('IN_CMS') or die('Direct access to Script restricted.');

class Geo extends Common
{
	

	/**
	 * Конструктор класса
	 */
	public function __construct()
	{

	}

	/**
	 * Деструктор класса
	 */
	public function __destruct()
	{

	}
	
	/**
	 * getCountryZones()
	 *
	 * Информация о всех районах / штатах выбранной страны
	 *
	 * @param	int		$country_id
	 */
	public function getCountryZones($country_id)
	{
		$zones = array();
		$n = 0;

		//
		$sql = "SELECT
					z.zone_id, 
					z.country_id, 
					z.code, 
					z.sort_order, 
					zl.zone_name 
				FROM
					" . TABLE_PREFIX . "countries_zones AS z
				LEFT JOIN
					" . TABLE_PREFIX . "countries_zones_lang AS zl
				ON
					z.zone_id = zl.zone_id
					AND
					zl.lang_id = " . LANG_ID . "
				WHERE
					z.country_id = " . (int)$country_id . "
				ORDER BY
					z.sort_order, 
					zl.zone_name";

		//self::$obj_error->showMessage($sql, 3);
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if ( self::$obj_db->sql_numrows($result) > 0 )
		{
			while ($row = self::$obj_db->sql_fetchrow($result))
			{
				$zones[$n]['zone_id']			= (int)$row['zone_id'];
				$zones[$n]['country_id']		= (int)$row['country_id'];
				$zones[$n]['code']				= self::$obj_functions->toHTML($row['code']);
				$zones[$n]['sort_order']		= (int)$row['sort_order'];
				$zones[$n]['zone_name']			= self::$obj_functions->toHTML($row['zone_name']);

				$n++;
			}
		}

		self::$obj_db->sql_freeresult($result);
		unset($sql, $result, $row, $n, $country_id);

		return $zones;
	}
	
	/**
	 * getCities()
	 *
	 * Информация о всех городах страны / района
	 *
	 * @param	int		$country_id
	 * @param	int		$zone_id
	 */
	public function getCities($country_id, $zone_id)
	{
		$cities = array();
		$n = 0;

		//
		$sql = "SELECT
					c.city_id, 
					c.code, 
					c.country_id, 
					c.zone_id,
					cl.city_name 
				FROM
					" . TABLE_PREFIX . "cities AS c
				LEFT JOIN
					" . TABLE_PREFIX . "cities_lang AS cl
				ON
					c.city_id = cl.city_id
					AND
					cl.lang_id = " . LANG_ID . " 
				WHERE
					c.country_id = " . (int)$country_id . " ";
		if ($zone_id > 0)
		{
			$sql.= " AND c.zone_id = " . (int)$zone_id . " ";
		}
		$sql.= "ORDER BY
					cl.city_name";

		//self::$obj_error->showMessage($sql, 3);
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if ( self::$obj_db->sql_numrows($result) > 0 )
		{
			while ($row = self::$obj_db->sql_fetchrow($result))
			{
				$cities[$n]['city_id']			= (int)$row['city_id'];
				$cities[$n]['code']				= self::$obj_functions->toHTML($row['code']);
				$cities[$n]['country_id']		= (int)$row['country_id'];
				$cities[$n]['zone_id']			= (int)$row['zone_id'];
				$cities[$n]['city_name']		= self::$obj_functions->toHTML($row['city_name']);

				$n++;
			}
		}

		self::$obj_db->sql_freeresult($result);
		unset($sql, $result, $row, $n, $country_id, $zone_id);

		return $cities;
	}
	
	/**
	 * Возвращает ID города по указанному коду
	 * 
	 * @param	string		$code
	 * @return	int			$city_id
	 */
	public function getCityByCode($code)
	{
		$city_id = 0;
		
		//
		$sql = "SELECT 
					city_id 
				FROM 
					" . TABLE_PREFIX . "cities 
				WHERE 
					code = " . self::$obj_functions->toMySQL($code) . " 
				LIMIT 1";
		
		//self::$obj_error->showMessage($sql, 3);
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if (self::$obj_db->sql_numrows($result) > 0)
		{
			$row = self::$obj_db->sql_fetchrow($result);
			$city_id = (int)$row['city_id'];
		}
		
		self::$obj_db->sql_freeresult($result);
		unset($code, $result, $row, $sql);
		
		return $city_id;
	}
	
	/**
	 * Информация о городе
	 *
	 * @param	int		$city_id
	 */
	public function getCityByID($city_id)
	{
		$city = array();

		//
		$sql = "SELECT
					c.city_id, 
					c.code, 
					c.country_id, 
					c.zone_id,
					c.map_x, 
					c.map_y, 
					c.map_zoom, 
					cl.city_name 
				FROM
					" . TABLE_PREFIX . "cities AS c
				LEFT JOIN
					" . TABLE_PREFIX . "cities_lang AS cl
				ON
					c.city_id = cl.city_id
					AND
					cl.lang_id = " . LANG_ID . "
				WHERE
					c.city_id = " . (int)$city_id . " 
				LIMIT 1";

		//self::$obj_error->showMessage($sql, 3);
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if ( self::$obj_db->sql_numrows($result) > 0 )
		{
			$row = self::$obj_db->sql_fetchrow($result);

			$city['city_id']		= (int)$row['city_id'];
			$city['code']			= self::$obj_functions->toHTML($row['code']);
			$city['country_id']		= (int)$row['country_id'];
			$city['zone_id']		= (int)$row['zone_id'];
			$city['map_x']			= (float)$row['map_x'];
			$city['map_y']			= (float)$row['map_y'];
			$city['map_zoom']		= (int)$row['map_zoom'];
			$city['city_name']		= self::$obj_functions->toHTML($row['city_name']);
		}

		self::$obj_db->sql_freeresult($result);
		unset($sql, $result, $row, $city_id);

		return $city;
	}
	
	/**
	 *  getLanguages()
	 * 
	 * Возвращает все языки
	 * 
	 * @return	array
	 */
	public function getLanguages()
	{
		$languages = array();
		$n = 0;

		//
		$sql = "SELECT
					l.lang_id, 
					l.code, 
					l.language, 
					ll.language_name 
				FROM
					" . TABLE_PREFIX . "languages AS l 
				LEFT JOIN
					" . TABLE_PREFIX . "languages_lang AS ll
				ON
					l.lang_id = ll.main_lang_id
					AND
					ll.lang_id = " . LANG_ID . " 
				WHERE
					ll.main_lang_id IS NOT NULL 
				ORDER BY 
					l.sort_order";

		//self::$obj_error->showMessage($sql, 3);
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if ( self::$obj_db->sql_numrows($result) > 0 )
		{
			while ($row = self::$obj_db->sql_fetchrow($result))
			{
				$languages[$n]['lang_id']			= (int)$row['lang_id'];
				$languages[$n]['code']				= self::$obj_functions->toHTML($row['code']);
				$languages[$n]['language']			= self::$obj_functions->toHTML($row['language']);
				$languages[$n]['language_name']		= self::$obj_functions->toHTML($row['language_name']);

				$n++;
			}
		}

		self::$obj_db->sql_freeresult($result);
		unset($sql, $result, $row, $n);

		return $languages;
	}
	
	
}
?>